Virginia Election Project

Author

Hannah Loder

Code
# cleaning up percentages 
joined_vacomparison <- joined_vacomparison %>% 
  mutate(youngkin_overperform = round_half_up(youngkin_overperform, 2),
         mcauliffe_underperform = round_half_up(mcauliffe_overperform, 2), 
         biden_overperform = round_half_up(biden_overperform, 2))

Charting top 5 counties where Youngkin outperformed Trump:

Youngkin outperformed Trump in every Virginia county in the 2020 election. Below is the process I did for charting the top 5 counties where Youngkin performed better than Trump, meaning the places where the difference between their percentages of the vote was greatest.

Code
toptencounties_YO <- joined_vacomparison %>% 
  arrange(desc(youngkin_overperform)) %>% 
  head(5)
Code
ggplot(toptencounties_YO, aes(x = locality, y = youngkin_overperform)) +
  geom_col(color = "#B32B2B", fill = "#B32B2B") +
  coord_flip() +
  scale_y_continuous(name = "Percent Overperform", labels = scales::comma) +
  scale_x_discrete(name = "County") +
  labs(title = "Where Youngkin Outperformed Trump (%)", 
       subtitle = "Top 5 VA Counties 2020",
       caption = "VA Department of Elections") +
  theme_fivethirtyeight()

Mapping Youngkin’s Margin of Victory over Trump

Below is a map showing the difference in the percentage of votes Youngkin received in Virginia counties and those that Trump received. The map uses shading to show the degree to which Youngkin outperformed Trump in every county.

Code
 census_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", install=TRUE, overwrite=TRUE) 
Your original .Renviron will be backed up and stored in your R HOME directory if needed.
Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). 
To use now, restart R or run `readRenviron("~/.Renviron")`
[1] "2a6f8c21a30d3024e038d67d7d4eba647dc79cd4"
Code
myvars <- c(totalpop = "B01003_001",
            medincome = "B19013_001",
            medage = "B01002_001"
)
Code
va_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       state = "VA",
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |======================================================================| 100%
Code
va_counties_withgeo
Simple feature collection with 133 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS:  NAD83
First 10 features:
   GEOID                         NAME totalpopE totalpopM medincomeE medincomeM
1  51105         Lee County, Virginia     22482        NA      37574       4430
2  51163  Rockbridge County, Virginia     22663        NA      57828       3580
3  51001    Accomack County, Virginia     33388        NA      50601       5803
4  51099 King George County, Virginia     26597        NA     101599       7080
5  51185    Tazewell County, Virginia     40615        NA      42937       3086
6  51029  Buckingham County, Virginia     16832        NA      49841       5835
7  51047    Culpeper County, Virginia     52021        NA      85274       5034
8  51179    Stafford County, Virginia    154204        NA     119818       3302
9  51085     Hanover County, Virginia    109232        NA      96911       2936
10 51009     Amherst County, Virginia     31381        NA      60876       3561
   medageE medageM                       geometry
1     45.6     0.7 MULTIPOLYGON (((-83.67461 3...
2     49.2     0.6 MULTIPOLYGON (((-79.68695 3...
3     47.1     0.6 MULTIPOLYGON (((-75.74241 3...
4     38.3     0.4 MULTIPOLYGON (((-77.34744 3...
5     45.1     0.3 MULTIPOLYGON (((-81.90089 3...
6     43.0     0.8 MULTIPOLYGON (((-78.83284 3...
7     39.0     0.6 MULTIPOLYGON (((-78.22926 3...
8     35.9     0.2 MULTIPOLYGON (((-77.63422 3...
9     42.1     0.4 MULTIPOLYGON (((-77.79681 3...
10    44.9     0.5 MULTIPOLYGON (((-79.43695 3...
Code
va_counties_withgeo <- va_counties_withgeo %>% 
  mutate(
    NAME = str_replace(NAME, ", Virginia", ""),
    NAME = str_to_upper(NAME)
  )
Code
joined <- left_join(va_counties_withgeo, joined_vacomparison, by = c("NAME" = "locality"))
Code
mylabel <- glue::glue("{joined$NAME} {joined$youngkin_overperform}")

mypopup <- glue::glue("<strong>{joined$NAME}</strong><br />
                      Percent: {joined$youngkin_overperform}<br />
                      Total Population: {joined$totalpopE}" ) %>% 
  lapply(htmltools::HTML)

mapview(joined, zcol = "youngkin_overperform", 
         col.regions = RColorBrewer::brewer.pal(9, "Reds"), 
         alpha.regions = 1,
         popup = mypopup, label = mylabel)
Warning: Found less unique colors (9) than unique zcol values (120)! 
Interpolating color vector to match number of zcol values.

Charting top 5 counties where Biden outperformed McAuliffe:

McAuliffe did not outperform Biden in any county, as referenced in the data. For this reason, I decided to chart the top ten counties where Biden outperformed McAuliffe.

Code
toptencounties_BO <- joined_vacomparison %>% 
  arrange(desc(biden_overperform)) %>% 
  head(5)
Code
ggplot(toptencounties_BO, aes(x = locality, y = biden_overperform)) +
  geom_col(color = "#287AC7", fill = "#287AC7") +
  coord_flip() +
  scale_y_continuous(name = "Percent Overperform", labels = scales::comma) +
  scale_x_discrete(name = "County") +
  labs(title = "Where Biden Outperformed McAuliffe (%)", 
       subtitle = "Top 5 VA Counties 2020",
       caption = "VA Department of Elections") +
  theme_fivethirtyeight()

Mapping Biden’s Margin of Victory over McAuliffe

I mapped the difference in the percentage of votes Biden received compared to those McAuliffe received. Biden outperformed McAuliffe in every Virginia county. The darker the shading, the greater the victory.

Code
map2label <- glue::glue("{joined$NAME} {joined$biden_overperform}")

popupmap2 <- glue::glue("<strong>{joined$NAME}</strong><br />
                      Percent: {joined$biden_overperform}<br />
                      Total Population: {joined$totalpopE}" ) %>% 
  lapply(htmltools::HTML)

mapview(joined, zcol = "biden_overperform", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1,
         label = map2label, popup = popupmap2)
Warning: Found less unique colors (9) than unique zcol values (115)! 
Interpolating color vector to match number of zcol values.

Charting Where Youngkin Got the Highest Percentage of the Vote

Youngkin won the Virginia Governor race in 2020. Below is the code I used to create a chart showing the five counties in Virginia where Youngkin got the highest percentage of the vote.

Code
youngkin_top5 <- joined_vacomparison %>% 
  arrange(desc(pct_youngkin)) %>% 
  head(5)
Code
ggplot(youngkin_top5, aes(x = locality, y = pct_youngkin)) +
  geom_col(color = "#FF3636", fill = "#FF3636") +
  coord_flip() +
  scale_y_continuous(name = "Percent Youngkin", labels = scales::comma) +
  scale_x_discrete(name = "County") +
  labs(title = "Youngkin Percent of Votes", 
       subtitle = "Top 5 VA Counties 2020",
       caption = "VA Department of Elections") +
  theme_fivethirtyeight()

Charting Where McAuliffe Got the Highest Percentage of the Vote

While McAuliffe did not won the Virginia Governor race, he did receive the highest percentage of the vote in some counties. I created a chart showing the top five counties in Virginia where McAuliffe got the highest percentage of the vote.

Code
mcauliffe_top5 <- joined_vacomparison %>% 
  arrange(desc(pct_mcauliffe)) %>% 
  head(5)
Code
ggplot(mcauliffe_top5, aes(x = locality, y = pct_mcauliffe)) +
  geom_col(color = "#4CA0FF", fill = "#4CA0FF") +
  coord_flip() +
  scale_y_continuous(name = "Percent McAuliffe", labels = scales::comma) +
  scale_x_discrete(name = "County") +
  labs(title = "McAuliffe Percent of Votes", 
       subtitle = "Top 5 VA Counties 2020",
       caption = "VA Department of Elections") +
  theme_fivethirtyeight()

Mapping Voting Percentages of Youngkin and McAuliffe

Below is a map showing the percentages of the vote Youngkin and McAuliffe received in Virginia counties. The maps of each of their performances are joined together to make one interactive map.

Code
youngkin_label <- glue::glue("{joined$NAME} {joined$pct_youngkin}")

popup_youngkin <- glue::glue("<strong>{joined$NAME}</strong><br />
                      Percent Youngkin: {joined$pct_youngkin}<br />
                      Total Population: {joined$totalpopE}" ) %>% 
  lapply(htmltools::HTML)


mcauliffe_label <- glue::glue("{joined$NAME} {joined$pct_mcauliffe}")

popup_mcauliffe <- glue::glue("<strong>{joined$NAME}</strong><br />
                      Percent McAuliffe: {joined$pct_mcauliffe}<br />
                      Total Population: {joined$totalpopE}" ) %>% 
  lapply(htmltools::HTML)

map_youngkin <- mapview(joined, zcol = "pct_youngkin", 
         col.regions = RColorBrewer::brewer.pal(9, "Reds"), 
         alpha.regions = 1, 
         label = youngkin_label, popup = popup_youngkin)
Warning: Found less unique colors (9) than unique zcol values (130)! 
Interpolating color vector to match number of zcol values.
Code
map_mcauliffe <- mapview(joined, zcol = "pct_mcauliffe", 
         col.regions = RColorBrewer::brewer.pal(9, "Blues"), 
         alpha.regions = 1, 
         label = mcauliffe_label, popup = popup_mcauliffe)
Warning: Found less unique colors (9) than unique zcol values (132)! 
Interpolating color vector to match number of zcol values.
Code
map_mcauliffe | map_youngkin